package TUILayer;
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.DVDCtr;
import ModelLayer.*;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    
    /**
     * Constructor for objects of this class.
     */
    public DvdUI()
    {
        dvdCtr = new DVDCtr();
    }
    
    /**
     * Start the showing of the dvd Menu.
     */
    public void start()
    {
        clscr();
        dvdMenu();
    }
    
    private void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            int choise =   writeDVDMenu();
            switch(choise)
            {
                case 1:
                    findDVD();
                    break;
                case 2:
                    findCopy();
                    break;
                case 3:
                    createDVD();
                    break;
                case 4:
                    createCopy();
                    break;
                case 5:
                    deleteDVD();
                    break;
                case 6: 
                    deleteCopy();
                    break;
                case 7:
                    listAllDvds();
                    break;
                case 8:
                    exit = true;
                    break;
                default:
                    invalidChoise();
                    break;
            }
        }//end while
    }
    
    private int writeDVDMenu()
    {
            System.out.println("");
            System.out.println("");
            System.out.println(" *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Show DVD specimen information");
            System.out.println(" (3) Create DVD description");
            System.out.println(" (4) Create DVD specimen");
            System.out.println(" (5) Delete DVD");
            System.out.println(" (6) Delete DVD specimen");
            System.out.println(" (7) Show current DVD list");
            System.out.println(" (8) Return");
            
            int choise = 0;
            while(choise == 0)
            {
                try
                {
                    Scanner keyboard = new Scanner(System.in);
                    System.out.print("\n Make your choise: ");
                    choise = keyboard.nextInt();
                }
                catch(Exception e)
                {
                    System.out.println("Invalid input, try again.");
                }
            }
            return choise;
    }
    
    //support method for clearing the terminal screen.
    private void clscr()
    {
        System.out.println("\f");
    }
    
    private long inputLong(String str)
    {
        long input = 0;
        while(input == 0)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                System.out.println(str);
                input = keyboard.nextLong();
            }
            catch(Exception e)
            {
                System.out.println("Invalid input, try again.");
            }
        }
        return input;
    }
        
    private String inputLine(String str)
    {
        String input = "";
        while(input.equals(""))
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                System.out.println(str);
                input = keyboard.nextLine();
            }
            catch(Exception e)
            {
                System.out.println("Invalid input, try again.");
            }
        }
        return input;
    }
    
    private double inputDouble(String str)
    {
        double input = 0;
        while(input == 0)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                System.out.println(str);
                input = keyboard.nextDouble();
            }
            catch(Exception e)
            {
                System.out.println("Invalid input, try again.");
            }
        }
        return input;
    }
    
    private void showAllDVDDetails(ArrayList<DVD> list)
    {
        for(DVD d : list)
        {
            System.out.println("");
            showDVDDetails(d);
        }
    }
    
    private void showDVDDetails(DVD dvd)
    {
        System.out.println("ID:" + dvd.getID());
        System.out.println("Title:" + dvd.getTitle());
        System.out.println("Director:" + dvd.getDirector());
        System.out.println("Release date:" + dvd.getReleaseDate());
        for(Copy c : dvd.listAllCopies())
        {
            showCopyDetails(c);
        }
    }
    
    private void showCopyDetails(Copy c)
    {
        System.out.println("    Serial number:" + c.getSerialNumber());
        System.out.println("    Acquisition date:" + c.getAcquisitionDate());
        System.out.println("    Acquisition price:" + c.getAcquisitionPrice());
    }
    
    private void findDVD()
    {
        long id = inputLong("Type DVD number: ");
        clscr();
        try
        {
            showDVDDetails(dvdCtr.findDVD(id));
        }
        catch(Exception e)
        {
            System.out.println("No such DVD found!");
        }
    }
    
    private void findCopy()
    {
        long id = inputLong("Type DVD number: ");
        String sn = inputLine("Type the copy serial number");
        clscr();
        try
        {
            showCopyDetails(dvdCtr.findCopy(id,sn));
        }
        catch(Exception e)
        {
            System.out.println("No such copy found!");
        }
    }
    
    private void createDVD()
    {
        String title = inputLine("Type the DVD title: ");
        String director = inputLine("Type the DVD director: ");
        String release = inputLine("Type the DVD release date: ");
        dvdCtr.createDVD(title,director,release);
        clscr();
        System.out.println("A new dvd was created");
    }
    
    private void createCopy()
    {
        long id = inputLong("Type DVD number: ");
        String sn = inputLine("Type copy serial number: ");
        String date = inputLine("Type the copy acquisition date: ");
        double price = inputDouble("Type the copy acquisition price ");
        clscr();
        if(dvdCtr.hasDvd(id))
        {
            dvdCtr.createCopy(id, sn, date, price);
            System.out.println("A new copy of dvd + " + id + ", was created.");
        }
        else
        {
            System.out.println("Colud not find DVD.");
        }
    }
    
    private void deleteDVD()
    {
        long id = inputLong("Type DVD number: ");
        boolean succes = dvdCtr.deleteDVD(id);
        clscr();
        if(succes)
        {
            System.out.println("Dvd " + id + " was deleted.");
        }
        else
        {
            System.out.println("Could not find dvd: " + id );
        }
    }
    
    private void deleteCopy()
    {
        long id = inputLong("Type DVD number: ");
        String sn = inputLine("Type copy serial number: ");
        boolean success = dvdCtr.deleteCopy(id,sn);
        clscr();
        if(success)
        {
            System.out.println("copy " + sn + "was deleted from dvd " + id);
        }
        else
        {
            System.out.println("Could not find copy: " + sn );
        }
    }
    
    private void listAllDvds()
    {
        ArrayList<DVD> list = dvdCtr.listAllDvds();
        clscr();
        showAllDVDDetails(list);
    }
    
    private void invalidChoise()
    {
    }
}